package com.ttxs.uaa.api.common;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ttxs.common.core.vo.R;
import com.ttxs.uaa.api.BaseApi;
import com.ttxs.common.core.constant.Api;
import com.ttxs.uaa.entity.SysDept;
import com.ttxs.uaa.entity.SysUser;
import com.ttxs.uaa.service.ISysDeptService;
import com.ttxs.uaa.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author john.yi@qq.com
 * @since 2021-11-20
 */
@RestController
@RequestMapping(Api.Common.Dept.PREFIX)
@RequiredArgsConstructor
public class DeptApi extends BaseApi {
    private final ISysDeptService sysDeptService;
    private final ISysUserService sysUserService;

    @GetMapping
    public R list(@RequestParam("page") int pageNo,
                  @RequestParam("limit") int pageSize) {
        Page<SysDept> page = sysDeptService.page(Page.of(pageNo, pageSize),
                new LambdaQueryWrapper<SysDept>()
                        .eq(SysDept::getPlatformId, getPlatformId()));
        return R.ok(page);
    }

    @GetMapping(Api.Common.Dept.LIST_ALL)
    public R listAll(@RequestParam(value = "deptName", required = false) String deptName,
                     @RequestParam(value = "leader", required = false) String leader,
                     @RequestParam(value = "phone", required = false) String phone,
                     @RequestParam(value = "status", required = false) String status) {

        List<SysDept> list = sysDeptService.list(new LambdaQueryWrapper<SysDept>()
                .eq(SysDept::getPlatformId, getPlatformId())
                .like(StringUtils.hasText(deptName), SysDept::getDeptName, deptName)
                .like(StringUtils.hasText(leader), SysDept::getLeader, leader)
                .like(StringUtils.hasText(phone), SysDept::getPhone, phone)
                .eq(StringUtils.hasText(status), SysDept::getStatus, status)
                .orderByAsc(SysDept::getParentId)
        );
        return R.ok(list);
    }

    @GetMapping("/{id}")
    public R get(@PathVariable Long id) {
        SysDept sysDept = this.sysDeptService.getById(id);
        return R.ok(sysDept);
    }

    @PostMapping
    public R add(@RequestBody SysDept sysDept) {
        sysDept.setPlatformId(getPlatformId());
        this.sysDeptService.save(sysDept);
        return R.ok();
    }

    @DeleteMapping("/{id}")
    public R del(@PathVariable("id") Long[] id) {
        long count = sysDeptService.count(new LambdaQueryWrapper<SysDept>().in(SysDept::getParentId, id));
        if(count > 0) {
            return R.error("无法删除！部门下有子部门");
        }
        count = sysUserService.count(new LambdaQueryWrapper<SysUser>().in(SysUser::getDeptId, id));
        if(count > 0) {
            return R.error("无法删除！部门下有" + count + "个员工");
        }
        this.sysDeptService.removeByIds(Arrays.asList(id));
        return R.ok();
    }

    @PutMapping("/{id}")
    public R update(@PathVariable Long id, @RequestBody SysDept sysDept) {
        sysDept.setDeptId(id);
        this.sysDeptService.saveOrUpdate(sysDept);
        return R.ok();
    }
}
